<template>
  <div class="container">
    <div class="left">
      <!-- 卡片视图区域 -->
      <el-card>
        <h3>学生列表</h3>
        <!-- 搜索与添加区域 -->
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入搜索内容" v-model="queryInfo.value"
              @keyup.enter.native="getStudent" clearable>
              <el-button slot="append" icon="el-icon-search"
                @click="getStudent"></el-button>
            </el-input>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-table :data="studentList" style="width: 100%" border>
          <el-table-column prop="_id" label="id" width="180">
          </el-table-column>
          <!-- 姓名 -->
          <el-table-column prop="name" label="姓名" width="180">
            <template width="90" slot-scope="scope">
              <el-input type="text" v-model="changeStudent.name"
                v-show="scope.row.isEdit" />
              <span v-show="!scope.row.isEdit">{{ scope.row.name }}</span>
            </template>
          </el-table-column>
          <!-- 年龄 -->
          <el-table-column prop="age" label="年龄" width="180">
            <template width="90" slot-scope="scope">
              <el-input type="text" v-model="changeStudent.age"
                v-show="scope.row.isEdit" />
              <span v-show="!scope.row.isEdit">{{ scope.row.age }}</span>
            </template>
          </el-table-column>
          <!-- 性别 -->
          <el-table-column prop="gender" label="性别">
            <template width="90" slot-scope="scope">
              <el-input type="text" v-model="changeStudent.gender"
                v-show="scope.row.isEdit" />
              <span v-show="!scope.row.isEdit">{{ scope.row.gender }}</span>
            </template>
          </el-table-column>
          <!-- 班级 -->
          <el-table-column prop="classesId" label="班级">
            <template width="90" slot-scope="scope">
              <span>{{ 
                scope.row.classesId ? scope.row.classesId.name : ""
                }}</span>
            </template>
          </el-table-column>
          <!-- 专业 -->
          <el-table-column label="专业">
            <template width="90" slot-scope="scope">
              <span>{{
                scope.row.subjectsId ? scope.row.subjectsId.name : ""
              }}</span>
            </template>
          </el-table-column>
          <!-- 头像 -->
          <el-table-column prop="image" label="头像">
            <template width="90" slot-scope="scope">
              <img style="width: 55px; height: 55px; border: none"
                :src="scope.row.image" v-show="!scope.row.isEdit" />
              <input type="file" @change="upImg" v-show="scope.row.isEdit" />
            </template>
          </el-table-column>
          <!-- 操作 -->
          <el-table-column prop="gender" label="操作" width="200">
            <template slot-scope="scope">
              <!-- 删除 -->
              <el-button v-auth size="mini" type="danger" icon="el-icon-delete"
                @click="deleteStudent(scope.row._id)">删除</el-button>
              <!-- 修改 -->
              <el-button size="mini" type="primary" v-show="!scope.row.isEdit"
                icon="el-icon-edit" @click="change(scope.row._id)">
                修改
              </el-button>
              <!-- 保存 -->
              <el-button size="mini" type="success" v-show="scope.row.isEdit"
                @click="upChange"><i
                  class="el-icon-upload el-icon--right"></i>保存</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="pages">
          <el-pagination @size-change="changeSize"
            @current-change="currentChange"
            :current-page="queryInfo.currentPage" :page-sizes="[1, 2, 5, 10]"
            :page-size="queryInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState: imgState, mapActions: imgAction } = createNamespacedHelpers("upimg");
export default {
  data() {
    return {
      studentList: [],
      changeStudent: {},
      changeNum: 0,
      queryInfo: { pageSize: 5, currentPage: 1, value: "" },
      total: 0,
    };
  },
  computed: {
    ...imgState(['imgsrc'])
  },
  methods: {
    // 图片上传
    ...imgAction(["upImg"]),
    // 获取学生列表
    async getStudent() {
      // 发请求
      let { data } = await this.$api.studentApi.getStudent({
        ...this.queryInfo,
      });
      // 处理
      this.total = data.data.total;
      // console.log(data.data);
      // 给获取到的数组加属性
      let arr = data.data.rows;
      arr.forEach((v) => {
        v.isEdit = false;
      });
      this.studentList = arr;
    },
    //删除
    deleteStudent(_id) {
      this.$api.studentApi.deleteStudent(_id).then(({ data }) => {
        // 删除提示
        this.$message.success(data.message);
        this.getStudent();
      });
    },
    // 修改
    change(id) {
      if (this.changeNum) return this.$message.error("请先完成当前修改");
      this.changeNum = 1;
      let res = this.studentList.filter((v) => v._id == id)[0];
      res.isEdit = true;
      this.changeStudent = { ...res };
    },
    // 确认修改
    upChange() {
      this.$api.studentApi
        .changeStudent({ ...this.changeStudent, image: this.imgsrc })
        .then(({ data }) => {
          this.$message.success(data.message);
          this.getStudent();
          this.changeNum = 0;
        });
    },
    // 每一页展示数量
    changeSize(i) {
      this.queryInfo.pageSize = i;
    },
    // 页数
    currentChange(i) {
      this.queryInfo.currentPage = i;
    },
  },
  created() {
    this.getStudent();
  },
  watch: {
    queryInfo: {
      handler() {
        this.getStudent();
      },
      deep: true
    }
  },
};
</script>

<style lang="scss" scoped>
img {
  width: 50px;
  height: 50px;
}

input {
  width: 100px;
}
</style>
